package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("del_WebService")
public class Del_WebService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;
    ResultSet rs = null;
    BaseDao baseDao = null;
    String sql = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }
    public boolean del(String ids){
        String[] arr = ids.split(",");
        init();
        boolean result = false;
        try {
            for (int i = 0; i < arr.length; i++) {
                String one = arr[i];
                String[] a = one.split("_");
                if (a.length == 3) {
                    String hostName = a[0];
                    String webTypeId = a[1];
                    String webTypeName = a[2];
                    sql = "delete from Webcategory as we where we.id.hostName =? and we.id.webTypeId = ? and we.webTypeName = ?";
                    Object[] params = {hostName,Integer.parseInt(webTypeId),webTypeName};
                    rs=baseDao.executeSQL(sql,params);
                }
            }
            result = true;
        } finally {
            close();
            return result;

        }
    }

//        String[] arr = ids.split(",");
//
//        Session session = hibernateSessionFactory.openSession();
//        session.clear();
//        boolean result = false;
//        try {
//            Transaction tx = session.beginTransaction();
//            for (int i = 0; i < arr.length; i++) {
//                String one = arr[i];
//                String[] a = one.split("_");
//                if (a.length == 3) {
//                    String hostName = a[0];
//                    String webTypeId = a[1];
//                    String webTypeName = a[2];
//                    String hql = "delete Webcategory as we where we.id.hostName =? and we.id.webTypeId = ? and we.webTypeName = ?";
//                    Query query = session.createQuery(hql);
//                    query.setString(0, hostName);
//                    query.setInteger(1, Integer.parseInt(webTypeId));
//                    query.setString(2, webTypeName);
//                    query.executeUpdate();
//                }
//            }
//            result = true;
//            tx.commit();
//        } finally {
//
//            session.close();
//            return result;
//
//        }
//    }
}
